package com.ckf.login_wx.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ckf.login_wx.entity.Material;
import com.ckf.login_wx.entity.Odd;
import com.ckf.login_wx.servic.MaterialService;
import com.ckf.login_wx.util.Idname;
import com.github.pagehelper.PageHelper;

@RestController
public class Materialcontroller {
	@Autowired
    private MaterialService materialService;
	@GetMapping("/addord")
    public Map<String, Object> addord(String name, String type,String time, String unite,String saisie,String modele, int money,int quantite) throws ParseException{
        Map<String, Object> map = new HashMap<>();
        Material material=new Material();
        material.setName(name);
        material.setModele(modele);
        material.setQuantite(quantite);
        material.setType(type);
        material.setMoney(money);
        material.setTotal(quantite*money);
        material.setUnite(unite);
        material.setSaisie(saisie);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        material.setTime(simpleDateFormat.parse(time));
		if (materialService.save(material)){
            map.put("code",200);
            map.put("result","添加成功");
            System.out.println("添加成功");
        }else {
        	map.put("code",200);
            map.put("result","no");
        }
        return map;
    }
    @GetMapping("/ordlist")
    public Object ordlist(){
        Map<String, Object> map = new HashMap<>();
        List<Material> list=materialService.list();
        map.put("list", list);
        double sum=0.00;int quantite =0;
        for (int i = 0; i < list.size(); i++) {
        	sum+=list.get(i).getTotal();
        	quantite+=list.get(i).getQuantite();
		}
        map.put("sum", sum);
        map.put("quantite", quantite);
        return map;
    }
    @GetMapping("/ordlistbysaisie")
    public Object ordlistbysaisie(String pagenew,String saisie){
    	Integer i = Integer.parseInt(pagenew); 
    	QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
        Map<String, Object> mapuser = new HashMap<>();
        mapuser.put(Idname.getname("saisie"),saisie);
        queryWrapper.allEq(mapuser);
    	System.out.println(materialService.list(queryWrapper).size()+"***");
   	 	PageHelper.offsetPage((i-1)*5, 5);
        return materialService.list(queryWrapper);
    }
    @GetMapping("/ordlistall")
    public Object ordlistall(){
        return materialService.list();
    }
    @GetMapping("/ordlistallbysaisie")
    public Object ordlistallbysaisie(String saisie){
    	QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
        Map<String, Object> mapuser = new HashMap<>();
        mapuser.put(Idname.getname("saisie"),saisie);
        queryWrapper.allEq(mapuser);
        return materialService.list(queryWrapper);
    }
    @GetMapping("/delematerialbyid")
    public Object delematerialbyid(Integer id,String pagenew){
    	materialService.removeById(id);
        Integer i = Integer.parseInt(pagenew);
    	System.out.println(materialService.list().size()+"***");
   	 	PageHelper.offsetPage((i-1)*5, 5);
        return materialService.list();
    }
    @GetMapping("/materialsearch")
    public Object materialsearch(String talk,String search){
        Map<String, Object> map = new HashMap<>();
        List<Material> list=materialService.list(new QueryWrapper<Material>().like(Idname.getname(search), talk));
        map.put("list", list);
        double sum=0.00;int quantite =0;
        for (int i = 0; i < list.size(); i++) {
        	sum+=list.get(i).getTotal();
        	quantite+=list.get(i).getQuantite();
		}
        map.put("sum", sum);
        map.put("quantite", quantite);
        return map;
    }
    @GetMapping("/ordmaterialbysearch")
    public Object ordmaterialbysearch(String search,String talk,String pagenew){
        Integer i = Integer.parseInt(pagenew);
    	if(search.equals("时间")){
       	 	PageHelper.offsetPage((i-1)*5, 5);
    		return materialService.seletime(talk);
    	}
//        QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
//        Map<String, Object> mapuser = new HashMap<>();
//        mapuser.put(Idname.getname(search),talk);
//        queryWrapper.allEq(mapuser);
   	 	PageHelper.offsetPage((i-1)*5, 5);
        return materialService.selectlike(Idname.getname(search), talk);
    }
    @GetMapping("/ordmaterialbysearchall")
    public Object ordmaterialbysearchall(String search,String talk){//
    	if(search.equals("时间")){
    		return materialService.seletime(talk);
    	}
//        QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
//        Map<String, Object> mapuser = new HashMap<>();
//        mapuser.put(Idname.getname(search),talk);
//        queryWrapper.allEq(mapuser);
        return materialService.selectlike(Idname.getname(search), talk);
    }
    @GetMapping("/quxiansearch")
    public Object quxiansearch(String search,String talk,String pagenew,String saisie){//
        Integer i = Integer.parseInt(pagenew);
    	if(search.equals("时间")){
       	 	PageHelper.offsetPage((i-1)*5, 5);
    		return materialService.seletimebyquan(talk, saisie);
    	}
        QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
        Map<String, Object> mapuser = new HashMap<>();
        mapuser.put(Idname.getname(search),talk);
        mapuser.put("saisie",saisie);
        queryWrapper.allEq(mapuser);
   	 	PageHelper.offsetPage((i-1)*5, 5);
        return materialService.list(queryWrapper);
    }
    @GetMapping("/quxiansearchall")
    public Object quxiansearchall(String search,String talk,String saisie){//
    	if(search.equals("时间")){
    		return materialService.seletimebyquan(talk, saisie);
    	}
        QueryWrapper<Material> queryWrapper = new QueryWrapper<>();
        Map<String, Object> mapuser = new HashMap<>();
        mapuser.put(Idname.getname(search),talk);
        mapuser.put("saisie",saisie);
        queryWrapper.allEq(mapuser);
        return materialService.list(queryWrapper);
    }
    /*
    通过id查询
    */
    @GetMapping("/selematerialbyid")
    public Object selematerialbyid(String id){//
    	Integer idint = Integer.parseInt(id);
        return materialService.getById(idint);
    }
    /*
    通过ID修改
    */
    @PostMapping("/upmaterialbyid")
    public Object upmaterialbyid(String id,String name, String type,String time, String unite,String saisie,String modele, int money,int quantite) throws ParseException{//
    	Map<String, Object> map = new HashMap<>();
        Integer i = Integer.parseInt(id);
    	Material material=new Material();
    	material.setId(i);
    	material.setMoney(money);
    	material.setModele(modele);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        material.setTime(simpleDateFormat.parse(time));
    	material.setName(name);
    	material.setQuantite(quantite);
    	material.setSaisie(saisie);
    	material.setTotal(quantite*money);
    	material.setType(type);
    	material.setUnite(unite);
		if (materialService.updateById(material)){
            map.put("code",200);
            map.put("result","修改成功");
            System.out.println("修改成功");
        }else {
        	map.put("code",200);
            map.put("result","no");
        }
        return map;
    }
}
